package cn.i7baozh.hystrix.consumer.controller;

import cn.i7baozh.hystrix.consumer.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务降级一般用于客户端
 *
 */
@Slf4j
@RestController
@DefaultProperties(defaultFallback = "paymentInfoErrorFallback")
public class OrderController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("consumer/hystrix/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfoOk(id);
        log.info("ok:{}", result);
        return result;
    }

//    @HystrixCommand(fallbackMethod = "paymentInfoErrorFallback",
//            commandProperties = {
//                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_INTERRUPT_ON_TIMEOUT,
//                            value = "1500")
//            })
    @HystrixCommand
    @GetMapping("consumer/hystrix/error/{id}")
    public String paymentInfoError(@PathVariable("id")Integer id) {
        String result = paymentService.paymentInfoError(id);
        log.info("error:{}", result);
        return result;
    }

    public String paymentInfoErrorFallback() {
        return "客户端自己的降级实现";
    }
}
